package com.sti.framework.models.Basics.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sti.framework.core.security.jwt.JwtUtil;
import com.sti.framework.core.web.ResponseBodyEntity;
import com.sti.framework.models.System.data.MenuManage;
import com.sti.framework.models.System.data.MenuRoleMngae;
import com.sti.framework.models.System.service.MenuManageService;
import com.sti.framework.models.System.service.MenuRoleMngaeService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/Api/AuthMenu")
@Api(tags = "权限", value = "权限")
public class AuthMenuController {

    @Resource
    private MenuManageService menuManageService;
    @Resource
    private MenuRoleMngaeService menuRoleMngaeService;

    /**
     * 获取权限
     */
    @GetMapping(value = "/getAuthMenuListApi")
    @ApiOperation(value = "权限", notes = "权限")
    public ResponseBodyEntity getAuthMenuListApi(HttpServletRequest request) {
        String token = request.getHeader("token");
        Claims claims = JwtUtil.checkJWT(token);
        //获取角色ID
        Long roleId = claims.get("role_id", Long.class);
        LambdaQueryWrapper<MenuManage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<MenuRoleMngae> menuRoleMngaeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (roleId != null && !roleId.equals(23691354597501L)) {
            //获取角色对应的角色菜单关系数据
            menuRoleMngaeLambdaQueryWrapper.eq(MenuRoleMngae::getRoleId,roleId);
            List<MenuRoleMngae> list = menuRoleMngaeService.list(menuRoleMngaeLambdaQueryWrapper);
            if(CollectionUtils.isEmpty(list)){
                return ResponseBodyEntity.success(new ArrayList<>());
            }
            //菜单ID
            Set<Long> menuIdList = list.stream().map(MenuRoleMngae::getMenuId).collect(Collectors.toSet());
            //根据有的菜单ID获取菜单
            lambdaQueryWrapper.in(MenuManage::getId,menuIdList);
            lambdaQueryWrapper.isNull(MenuManage::getParentId);
            //一级菜单
            List<MenuManage> menuManageList = menuManageService.list(lambdaQueryWrapper);
            //获取二级菜单
            for (MenuManage menuManage : menuManageList) {
                lambdaQueryWrapper.clear();
                lambdaQueryWrapper.eq(MenuManage::getParentId,menuManage.getId());
                lambdaQueryWrapper.in(MenuManage::getId,menuIdList);
                menuManage.setChildren(menuManageService.list(lambdaQueryWrapper));
            }
            return ResponseBodyEntity.success(menuManageList);
        }else if(roleId != null && roleId.equals(23691354597501L)){
            lambdaQueryWrapper.isNull(MenuManage::getParentId);
            //一级菜单
            List<MenuManage> menuManageList = menuManageService.list(lambdaQueryWrapper);
            //获取二级菜单
            for (MenuManage menuManage : menuManageList) {
                lambdaQueryWrapper.clear();
                lambdaQueryWrapper.eq(MenuManage::getParentId,menuManage.getId());
                menuManage.setChildren(menuManageService.list(lambdaQueryWrapper));
            }
            return ResponseBodyEntity.success(menuManageList);
        }
        return ResponseBodyEntity.success(new ArrayList<>());
    }

}
